package cn.iocoder.yudao.module.system.controller.admin.userReal;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.userReal.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.userReal.UserRealDO;
import cn.iocoder.yudao.module.system.convert.userReal.UserRealConvert;
import cn.iocoder.yudao.module.system.service.userReal.UserRealService;

@Api(tags = "管理后台 - 用户实名制")
@RestController
@RequestMapping("/system/user-real")
@Validated
public class UserRealController {

    @Resource
    private UserRealService userRealService;

    @PostMapping("/create")
    @ApiOperation("创建用户实名制")
    @PreAuthorize("@ss.hasPermission('system:user-real:create')")
    public CommonResult<Long> createUserReal(@Valid @RequestBody UserRealCreateReqVO createReqVO) {
        return success(userRealService.createUserReal(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户实名制")
    @PreAuthorize("@ss.hasPermission('system:user-real:update')")
    public CommonResult<Boolean> updateUserReal(@Valid @RequestBody UserRealUpdateReqVO updateReqVO) {
        userRealService.updateUserReal(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户实名制")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:user-real:delete')")
    public CommonResult<Boolean> deleteUserReal(@RequestParam("id") Long id) {
        userRealService.deleteUserReal(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户实名制")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:user-real:query')")
    public CommonResult<UserRealRespVO> getUserReal(@RequestParam("id") Long id) {
        UserRealDO userReal = userRealService.getUserReal(id);
        return success(UserRealConvert.INSTANCE.convert(userReal));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户实名制列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:user-real:query')")
    public CommonResult<List<UserRealRespVO>> getUserRealList(@RequestParam("ids") Collection<Long> ids) {
        List<UserRealDO> list = userRealService.getUserRealList(ids);
        return success(UserRealConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户实名制分页")
    @PreAuthorize("@ss.hasPermission('system:user-real:query')")
    public CommonResult<PageResult<UserRealRespVO>> getUserRealPage(@Valid UserRealPageReqVO pageVO) {
        PageResult<UserRealDO> pageResult = userRealService.getUserRealPage(pageVO);
        return success(UserRealConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户实名制 Excel")
    @PreAuthorize("@ss.hasPermission('system:user-real:export')")
    @OperateLog(type = EXPORT)
    public void exportUserRealExcel(@Valid UserRealExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserRealDO> list = userRealService.getUserRealList(exportReqVO);
        // 导出 Excel
        List<UserRealExcelVO> datas = UserRealConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户实名制.xls", "数据", UserRealExcelVO.class, datas);
    }


    @PostMapping("/bind")
    @ApiOperation("创建用户实名制")
    @PreAuthorize("@ss.hasPermission('system:user-real:create')")
    public CommonResult<Long> createORupdateUserReal(@Valid @RequestBody UserRealCreateReqVO createReqVO) {
        return success(userRealService.createORupdateUserReal(createReqVO));
    }

}
